package com.jinandi.app.util.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.AntPathRequestMatcher;
import org.springframework.security.web.util.RequestMatcher;
import org.springframework.transaction.annotation.Transactional;

import com.jinandi.app.dao.AuthorityDao;
import com.jinandi.app.dao.ResourceDao;
import com.jinandi.app.domain.Authorit;
import com.jinandi.app.domain.Resource;

public class MyInvocationSecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {

	private ResourceDao resourceDao;

	private AuthorityDao authorityDao;

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}

	@Autowired
	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}

	public ResourceDao getResourceDao() {
		return resourceDao;
	}

	@Autowired
	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	private static Map<RequestMatcher, Collection<ConfigAttribute>> resourceMap = null;

	@Transactional
	public void loadResourceDefine() {//以后加缓存
		resourceMap = new HashMap<RequestMatcher, Collection<ConfigAttribute>>();
		List<Authorit> authorits = authorityDao.findAll();
		if (!authorits.isEmpty()) {
			for (Authorit authorit : authorits) {
				List<Resource> resources = resourceDao.findByAuthorit(authorit);
				Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
				ConfigAttribute ca = new SecurityConfig(authorit.getAuthority());
				atts.add(ca);
				for (Resource resource : resources) {
					AntPathRequestMatcher am = new AntPathRequestMatcher(
							resource.getUrl());
					//http method 以后加
					resourceMap.put(am, atts);
				}
			}
		}
	}

	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {


		return null;
	}

	@Override
	public Collection<ConfigAttribute> getAttributes(Object object)
			throws IllegalArgumentException {
//		if (null == resourceMap) {
			loadResourceDefine();
//		}
		
		final HttpServletRequest url = ((FilterInvocation) object).getRequest();
		for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : resourceMap
				.entrySet()) {
			if (entry.getKey().matches(url)) {
				return entry.getValue();
			}
		}
		return null;
	}

	@Override
	public boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		// return true;
		return FilterInvocation.class.isAssignableFrom(clazz);
	}

}
